use core_api::checksum::{md5, read_magic_md5, write_magic_md5, MAGIC_NUMBER, TAIL_LEN};
use std::io;

// cargo run --example testmd5
fn main() -> io::Result<()> {
    //let file1 = "E:/ai_model/llava-v1.5-7b-q4.llamafile";
    let  file2 ="D:/work20220906/gitee/rusttest/tauritest240709/dll/WindowsFormsApp1/WindowsFormsApp1/bin/Debug/xue.mp4";
    let file = file2;
    let is_skip = true;

    // 写入
    let magic_md5_r = read_magic_md5(file)?;
    if !magic_md5_r.starts_with(MAGIC_NUMBER) {
        println!("不是合法文件");
        // 计算
        let md5str = md5(file, is_skip, 0)?;
        println!("compute md5 :{}", md5str);
        let magic_md5_w = format!("{}{}", MAGIC_NUMBER, md5str);
        write_magic_md5(file, &magic_md5_w)?;
    }
    // 读取验证
    if magic_md5_r.starts_with(MAGIC_NUMBER) {
        let md5 = md5(file, is_skip, TAIL_LEN)?;
        assert_eq!(md5, magic_md5_r[MAGIC_NUMBER.len()..]);
        println!("check pass!!!,{}", md5);
    }
    Ok(())
}
